﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QUT.Bio.Tags {
	public partial class ObjectTagList : UserControl {
		private object subject;
		private List<Button> links = new List<Button>();

		public ObjectTagList() {
			InitializeComponent();
			plusButton.Click += PlusButtonClicked;
		}

		public object Subject {
			get {
				return subject;
			}
			set {
				if ( subject != value ) {
					subject = value;

					if ( Tags.AllTagsLoaded ) {
						Refresh();
					}
					else {
						Tags.WhenTagsLoaded += WhenTagsLoaded;
					}
				}
			}
		}

		public IObjectSelector ObjectSelector {
			get;
			set;
		}

		public Action<QUT.Bio.Util.PopupWindow> ShowWindow {
			get;
			set;
		}

		private void PlusButtonClicked( object sender, RoutedEventArgs args ) {
			try {
				ObjectTagAdder tagAdder = new ObjectTagAdder() {
					Subject = Subject,
					CurrentTags = Tags.GetTags( Subject ),
					ObjectSelector = ObjectSelector,
					PostProcess = Refresh,
				};

				ShowWindow( tagAdder.Dialog );
			}
			catch {
			}
		}

		private void WhenTagsLoaded() {
			Tags.WhenTagsLoaded -= WhenTagsLoaded;
			Refresh();
		}

		private void Refresh() {
			WrappedRdfTag[] tags = Tags.GetTags( subject ).ToArray();
			Array.Sort( tags );

			tagPanel.Children.Clear();

			foreach ( var t in tags ) {
				tagPanel.Children.Add( new Button() {
					Content = t.ToString(),
					Foreground = new SolidColorBrush( Colors.Green )
				} );
			}
		}
	}
}
